<template>
 <div class="app-container">
  <el-card style="height:80px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类编码" prop="sort_id">
        <el-input
          v-model="queryParams.sort_id"
          placeholder="请输入分类编码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类名称" prop="sort_name">
        <el-input
          v-model="queryParams.sort_name"
          placeholder="请输入分类名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>


   <el-card style="margin-top:20px">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['equipment:EquipmentClassification:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['equipment:EquipmentClassification:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['equipment:EquipmentClassification:remove']"
        >删除</el-button>
      </el-col>

    </el-row>

    <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分类编码" align="center" prop="sort_id" />
      <el-table-column label="分类名称" align="center" prop="sort_name"  />
      <el-table-column label="公司名称" align="center" prop="gs_name" />
      <el-table-column label="部门名称" align="center" prop="dept_name" />
      <el-table-column label="创建者" align="center" prop="create_by" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="0"
                inactive-value="1"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['equipment:EquipmentClassification:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['equipment:EquipmentClassification:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['equipment:EquipmentClassification:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  <!-- 添加或修改参数配置对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules">
                <el-row :gutter="80">
                    <el-col :span="8">
                        <el-form-item label="分类编码" prop="sort_id" :label-width="formLabelWidth">
                            <el-input v-model="form.sort_id" placeholder="请输入分类编号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="分类名称" prop="sort_name" :label-width="formLabelWidth">
                            <el-input v-model="form.sort_name" placeholder="请输入分类名称" />
                        </el-form-item>
                    </el-col>
                    
                    <el-col :span="8">
                        <el-form-item label="公司名称" prop="gs_name" :label-width="formLabelWidth">
                            <el-input v-model="form.gs_name" placeholder="请输入公司名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="资产部门" prop="dept_name" :label-width="formLabelWidth">
                            <el-input v-model="form.dept_name" placeholder="请输入部门名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="创建者" prop="create_by" :label-width="formLabelWidth">
                            <el-input v-model="form.create_by" placeholder="请输入创建者" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="状态" prop="status">
                            <el-select v-model="form.status" placeholder="请选择状态" size="small">
                            <el-option label="正常" value="正常"></el-option>
                            <el-option label="故障" value="故障"></el-option>
                        </el-select>
                        </el-form-item>
                    </el-col>

                </el-row>
                

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
  </el-card>
 </div>
</template>

<script>
import {addSort,updateSort,listSort,getSort} from "@/api/equipment/EquipmentClassification"
export default {
  name:'EquipmentClassification',
  data() {
    return {
      // 遮罩层
      loading: true,
     
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //状态
      value:true,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      //输入框大小
      formLabelWidth:'120px',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sort_id: undefined,
        sort_name: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sort_name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      listSort(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.typeList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        
        sort_id: undefined,
        sort_name: undefined,
        gs_name: undefined,
        create_by,undefined,
        dept_name:undefined,
        status: "0",
        // remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      this.open = true;
      this.title = "添加设备分类";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sort_id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sort_id = row.sort_id || this.ids
      getSort(sort_id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "设备分类";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      console.log(11);
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.form);
          if (this.form.sort_id != undefined) {
            updateSort(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
           
          } else {
            console.log(this.form);
            addSort(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
     // 用户状态修改
     handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.sort_name + '"分类吗？').then(function() {
        return changeUserStatus(row.sort_id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sort_ids = row.sort_id || this.ids;
      this.$modal.confirm('是否确认删除字典编号为"' + sort_ids + '"的数据项？').then(function() {
        return delType(sort_ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
}
</script>

<style>

</style>